package com.house365.core.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.house365.core.R;
import com.house365.core.image.AsyncImageLoader;

public class GalleryAdapter extends BaseCacheListAdapter<String> {
	private Gallery gallery;
	private int height=0;
	public GalleryAdapter(Context context) {
		super(context);
	}
	
	public Gallery getGallery() {
		return gallery;
	}

	public void setGallery(Gallery gallery) {
		this.gallery = gallery;
	}
	
	public int getHeight(){
		if(height == 0 && gallery!=null){
			height = gallery.getHeight();
		}
		return height;
	}

	@Override
	public View getView(int arg0, final View arg1, ViewGroup arg2) {
		final String url = getItem(arg0);
		ImageView i = new ImageView(context);
		if (url.endsWith(".thumb.jpg")) {
			setCacheImage(i, url, R.drawable.img_default_small,
					AsyncImageLoader.IMAGE_SCALE_TYPE_MIDDLE);
		} else {
			setCacheImage(i, url, R.drawable.img_default_big,
					AsyncImageLoader.IMAGE_SCALE_TYPE_HIGH);
		}
		i.setAdjustViewBounds(true); 
		i.setScaleType(ScaleType.FIT_XY);
		if(getHeight()>0){
			i.setLayoutParams(new Gallery.LayoutParams(getHeight(),
					getHeight()));
			}else{
				i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT));	
			}
		return i;
	}
}
